package com.daye.homework.servlet;

import com.daye.homework.bean.Student;
import com.daye.homework.factory.AdminServiceFactory;
import com.daye.homework.service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "UpdateStudentServlet" , urlPatterns = "/updateStudent")
public class UpdateStudentServlet extends HttpServlet {
    private AdminService adminService = AdminServiceFactory.getAdminService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));

        try {
            Student student = adminService.getStudentById(id);

            request.setAttribute("student" , student);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        request.getRequestDispatcher("updateStudentPage.jsp").forward(request , response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
